package fanjingzhu.check.action;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import publics.model.Application;
import publics.util.MyUtils;
import publics.util.Page;

import com.opensymphony.xwork2.ActionSupport;

import fanjingzhu.check.service.ApplicationService;

public class ApplicationAction extends ActionSupport{
	
	private static final long serialVersionUID = 1L;
	ApplicationService applicationService;  //spring注入进来的 companyService
//	private EnterpriseTypeDao enterpriseTypeDao;
	

	private boolean success;   //此参数为必须的， extjs根据这个参数的值判断是否成功执行操作
	private int limit;  // 每页多少个记录
	private int start;   //分页查询的 要查询数据的开始号 ，和offset值相同
	private String conditions;//查询条件（即搜索页面上文本框中输入的值），多个条件用空格分开
	private Page page;   //这是分页查询用的辅助类，用此类可以解决分页查询中要返回多个参数的问题，推荐使用

	private Application application;
	private Integer applicationId;
	
	public ApplicationService getApplicationService() {
		return applicationService;
	}
	@Resource(name="applicationService")
	public void setApplicationService(ApplicationService applicationService) {
		this.applicationService = applicationService;
		System.out.println("ApplicationServiceImpl inject in ApplicationAction!");
	}
	
	/**
	 * 添加贷款申请
	 * @return
	 */
	public String saveApplication() {
		System.out.println("已经进入 Action 的 saveApplication方法！");//测试用，可去掉
		applicationId = (Integer) applicationService.saveApplication(application);
		if (applicationId != null) {
			success = true;
		}else{
			success = false;  //失败返回
		}
		System.out.println("准备返回，success="+success);
		return SUCCESS;
	}
	
	/**
	 * 删除贷款申请
	 * 
	 * @return
	 */
	public String deleteApplication() {
		if(Integer.valueOf(applicationId) < 1){
			System.out.println("删除失败！ 贷款申请id为="+application.getId());
			success = false;
		}else{
			success = applicationService.deleteApplication(Integer.valueOf(applicationId));
		}
		return SUCCESS;
	}
	
	/**
	 * 修改贷款申请指定字段的值
	 * 
	 * @return
	 * @throws Exception
	 */
	public String updateApplication() throws Exception {
		
		if(Integer.valueOf(application.getId()) < 1){
			System.out.println("修改失败！贷款申请id为="+application.getId());
			success = false;
		}else{
			success = applicationService.updateApplication(application);
//			System.out.println("after更新公司success="+success);//测试用
		}
		return SUCCESS;
	}
	
	/**
	 * 根据条件分页查找所有贷款申请表
	 * @return
	 */
	public String findAllApplication() {
		page = new Page();
		page.setStart(start);
		page.setLimit(limit = (limit == 0) ? 20 : limit);
		page = applicationService.findByPage(page);  //查询 
//		page = enterpriseService.findByStatePage(page);
		if ((null != page.getRoot() && page.getTotalProperty() > 0 )||(null == page.getRoot() && page.getTotalProperty() == 0 )) {
			page.setSuccess(true);
			System.out.println("查询成功返回数据，准备返回。page.root="+page.getRoot());//测试用
		}else{
			success = false;
		}
		return SUCCESS;
	}
	
	/**
	 * 
	 * 根据id查找贷款申请
	 * @return
	 */
	public String findByAppId(){
		
		this.setApplication(applicationService.findById(applicationId));
		if (this.getApplication() != null) {
			success = true;
		}else{
			success = false;
		}
		System.out.println("已经查询到id号为"+applicationId+"的记录");
		return SUCCESS;
	}
	public boolean isSuccess() {
		return success;
	}
	public void setSuccess(boolean success) {
		this.success = success;
	}
	public int getLimit() {
		return limit;
	}
	public void setLimit(int limit) {
		this.limit = limit;
	}
	public int getStart() {
		return start;
	}
	public void setStart(int start) {
		this.start = start;
	}
	public String getConditions() {
		return conditions;
	}
	public void setConditions(String conditions) {
		this.conditions = conditions;
	}
	public Page getPage() {
		return page;
	}
	public void setPage(Page page) {
		this.page = page;
	}
	public Application getApplication() {
		return application;
	}
	public void setApplication(Application application) {
		this.application = application;
	}
	public Integer getApplicationId() {
		return applicationId;
	}
	public void setApplicationId(Integer applicationId) {
		this.applicationId = applicationId;
	}
	
	

}
